//
//  BJGoodsImageView.m
//  SZBank
//
//  Created by rimi on 16/7/21.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "BJGoodsImageView.h"
#import "BJScrollerViewController.h"
@interface BJGoodsImageView ()
{
    CGFloat frameWidth;/**< 自身宽 */
    CGFloat frameHeight;/**< 自身高 */
    NSMutableArray *controllerDataArray;/**< 控制器数据源数组 */
}
@property (nonatomic)UIScrollView *rightScrollView;/**< 右边滚动视图 */
@property (nonatomic)UIButton *closeButton;/**< 关闭按钮 */
@property (nonatomic)UIButton *imageButton;/**< 左边图片点击 */
@end
@implementation BJGoodsImageView

- (instancetype)initWithFrame:(CGRect)frame withDataDictionary:(NSDictionary *)dataDictionary
{
    self = [super initWithFrame:frame];
    if (self) {
        controllerDataArray = [NSMutableArray array];
       [ controllerDataArray addObject:dataDictionary[@"THUMBNAILURL"]];
        [controllerDataArray addObjectsFromArray:dataDictionary[@"DETAILSPIC"]];
        //高宽赋值
        frameWidth = self.frame.size.width;
        frameHeight = self.frame.size.height;
        //打开可编辑属性
        self.userInteractionEnabled = YES;
        self.image = [LocalDataManager getImageWithSourceOfPath:@"贵金属详情_01"];
        //添加视图
        [self addSubview:self.rightScrollView];
        [self addSubview:self.closeButton];
        
        
   
        
        
        [self.imageButton setBackgroundImage:[LocalDataManager getImageWithURL:dataDictionary[@"THUMBNAILURL"]] forState:UIControlStateNormal];
        [self addSubview:self.imageButton];
        [self addViewsToScrollViewWithDictionary:dataDictionary];
    }
    return self;
}



#pragma mark -- actions
- (void)action_clickButton:(UIButton *)sender{
    if (sender.tag == 1000) {
         [self removeFromSuperview];
    }else{
        BJScrollerViewController *controller = [[BJScrollerViewController alloc]init];
        controller.dataArray = controllerDataArray;
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:controller animated:YES completion:nil];
    }
   
    
}


- (void)addViewsToScrollViewWithDictionary:(NSDictionary *)dataDictionary{
    //获取滚动视图的宽高
    CGFloat scrWidth = self.rightScrollView.frame.size.width;
    CGFloat scrHeight = self.rightScrollView.frame.size.height;
    //添加标题背景图片
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, scrWidth, scrHeight * 0.06)];
    imageView.image = [LocalDataManager getImageWithSourceOfPath:@"贵金属详情_03"];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, scrWidth, scrHeight * 0.06)];
    titleLabel.text = dataDictionary[@"NAME"];
    titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
    titleLabel.textColor = [UIColor colorWithRed:213/255.0 green:158/255.0 blue:0 alpha:1];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [imageView addSubview:titleLabel];
    [self.rightScrollView addSubview:imageView];
        //添加动态label
    if (((NSArray *)dataDictionary[@"MATERIAL"]).count != 0 && ((NSString *)dataDictionary[@"SPECIFICATION"]).length != 0 &&  ((NSString *)dataDictionary[@"WEIGHT"]).length != 0 && ((NSString *)dataDictionary[@"PRICE"]).length != 0) {
        
        //重组数组
        NSMutableArray *nameArray_secrond = [NSMutableArray array];
        [nameArray_secrond addObject:[NSString stringWithFormat:@"成色：%@",dataDictionary[@"MATERIAL"][0]]];
        [nameArray_secrond addObject:[NSString stringWithFormat:@"规格/枚套：%@",dataDictionary[@"SPECIFICATION"]]];
        [nameArray_secrond addObject:[NSString stringWithFormat:@"克重(g)：%@",dataDictionary[@"WEIGHT"]]];
        [nameArray_secrond addObject:[NSString stringWithFormat:@"价格：%@",dataDictionary[@"PRICE"]]];
        //创建label
        for (int i  = 0; i < 4; i ++) {
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(scrWidth * 0.25, scrHeight * 0.7 + scrHeight * i * 0.06, scrWidth * 0.6, scrHeight * 0.06)];
            label.text = nameArray_secrond[i];
            label.font = [UIFont systemFontOfSize:FONT_SIZE];
            [self.rightScrollView addSubview:label];
        }

        
    }
    
    
    
    
    
    //介绍label
    UILabel *intruduceLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, scrHeight * 0.11, scrWidth * 0.9, scrHeight * 0.38)];
    intruduceLabel.numberOfLines = 0 ;
    intruduceLabel.font = [UIFont systemFontOfSize:24.0];
    intruduceLabel.text = dataDictionary[@"SUMMARY"];
    
    //设置首行缩进,行间距等
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString: dataDictionary[@"SUMMARY"]];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setFirstLineHeadIndent:scrWidth * 0.15];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, ((NSString *) dataDictionary[@"SUMMARY"]).length)];
    [paragraphStyle setLineSpacing:10];
    intruduceLabel.attributedText = attributedString;
    [intruduceLabel sizeToFit];
    
    [self.rightScrollView addSubview:intruduceLabel];
  
    //添加固定label
    NSArray *nameArray = @[@"介绍",@"规格",@"文档",@"说明书",@"条款",@"资料"];
    for (int i = 0; i < 6; i ++) {
        UILabel *label = [[UILabel alloc]init];
        label.text = nameArray[i];
        label.font = [UIFont systemFontOfSize:FONT_SIZE];
        if (i < 3) {
            
            if (i > 0) {
                label.frame = CGRectMake(0, CGRectGetMaxY(intruduceLabel.frame) +scrHeight * (i - 1) * 0.42 , scrWidth * 0.15, scrHeight * 0.06);
            }else{
                label.frame = CGRectMake(0, scrHeight * 0.1  , scrWidth * 0.15, scrHeight * 0.06);
            }
            label.backgroundColor = [UIColor colorWithRed:213/255.0 green:158/255.0 blue:0 alpha:1];
            label.textAlignment = NSTextAlignmentCenter;
            label.textColor =[UIColor whiteColor];
        }else{
            label.frame = CGRectMake(scrWidth * 0.25, CGRectGetMaxY(intruduceLabel.frame) +scrHeight  * 0.42 + (i - 3) * scrHeight * 0.06, scrWidth * 0.15, scrHeight * 0.06);
            label.textColor = [UIColor colorWithRed:213/255.0 green:158/255.0 blue:0 alpha:1];
        }
        [self.rightScrollView addSubview:label];
    }

    self.rightScrollView.contentSize = CGSizeMake(scrWidth, scrHeight * 2);
    
  
    
    
    
}

#pragma mark -- getters
- (UIScrollView *)rightScrollView{
    if (!_rightScrollView) {
        _rightScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(frameWidth * 0.6, frameHeight * 0.03, frameWidth * 0.38, frameHeight * 0.87)];
        _rightScrollView.showsVerticalScrollIndicator = YES;
        _rightScrollView.contentSize = CGSizeMake(frameWidth * 0.38, frameHeight);
        _rightScrollView.bounces = NO;
        
        
    }
    return _rightScrollView;
}
- (UIButton *)closeButton{
    if (!_closeButton) {
        _closeButton = [[UIButton alloc]initWithFrame:CGRectMake(frameWidth * 0.85, frameHeight * 0.9, frameWidth * 0.1, frameHeight * 0.06)];
        [_closeButton setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"贵金属详情_关闭"] forState:UIControlStateNormal];
        [_closeButton addTarget:self action:@selector(action_clickButton:) forControlEvents:UIControlEventTouchUpInside];
        _closeButton.tag = 1000;
    }
    return _closeButton;
}

- (UIButton *)imageButton{
    if (!_imageButton) {
        _imageButton = [[UIButton alloc]initWithFrame:CGRectMake(frameWidth * 0.05, frameHeight * 0.2, frameWidth * 0.5, frameHeight *0.5)];
        _imageButton.tag = 1001;
        [_imageButton addTarget:self action:@selector(action_clickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _imageButton;
}


@end
